package com.controller.user;


import com.javabean.Bill;
import com.javabean.Provider;
import com.javabean.Role;
import com.javabean.User;
import com.mysql.jdbc.StringUtils;
import com.service.bill.BillService;
import com.service.provider.ProviderService;
import com.service.role.RoleService;
import com.service.user.UserService;
import com.utils.Constants;
import com.utils.PageSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.List;

@Controller
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private BillService billService;
    @Autowired
    private ProviderService providerService;

    @RequestMapping("/login.do")
    public String getLoginUser(String userCode, String userPassword, Model model, HttpServletRequest request) {
        User User = userService.getLoginUser(userCode, userPassword);
        if (User == null) {
            model.addAttribute("error", "用户名或密码错误");
            return "login";
        } else {
            request.getSession().setAttribute(Constants.USER_SESSION, User);
            return "frame";
        }
    }

    @RequestMapping("/logout.do")
    public String exit(HttpServletRequest request) {
        request.getSession().removeAttribute(Constants.USER_SESSION);
        return "login";
    }

    @RequestMapping("/user.do")
    public String updatepwd(HttpServletRequest request, Model model) throws ServletException, IOException {
        String newpassword = request.getParameter("newpassword");
        User userSession = (User) request.getSession().getAttribute(Constants.USER_SESSION);
        if (userSession != null && !StringUtils.isNullOrEmpty(newpassword)) {
            userService.updatePwd(userSession.getId(), newpassword);
            model.addAttribute("message", "修改密码成功");
            request.getSession().removeAttribute(Constants.USER_SESSION);
            return "login";
        } else {
            model.addAttribute("message", "修改密码失败");
            return "pwdmodify";
        }
    }

    @RequestMapping("/query.do")
    public String showUserList(Model model, HttpServletRequest request) {
        String queryname = request.getParameter("queryname");
        String queryUserRoletemp = request.getParameter("queryUserRole");
        String pageIndex = request.getParameter("pageIndex");
        int queryUserRole = 0;

        if (queryname == null) {
            queryname = "";
        }

        if (queryUserRoletemp != null && !queryUserRoletemp.equals("")) {
            queryUserRole = Integer.parseInt(queryUserRoletemp);
        }
        //第一次进来显示第一页数据
        int pageSize = 5;
        int currentPageNo = 1;
        if (pageIndex != null) {
            currentPageNo = Integer.parseInt(pageIndex);
        }
        //获取用户总数
        Integer userTotalCount = userService.getUserCount(queryname, queryUserRole);

        PageSupport pageSupport = new PageSupport();
        pageSupport.setCurrentPageNo(currentPageNo);
        pageSupport.setPageSize(pageSize);
        pageSupport.setTotalCount(userTotalCount);

        //获取总页数
        int totalPageCount = pageSupport.getTotalPageCount();
        //控制首页和尾页
        if (currentPageNo < 1) {
            currentPageNo = 1;
        } else if (currentPageNo > totalPageCount) {
            currentPageNo = totalPageCount;
        }

        List<Role> roleList = roleService.getRoleList();
        model.addAttribute("roleList", roleList);
        for (Role role : roleList) {
            model.addAttribute("role", role);
        }

        List<User> userList = userService.getUserList(queryname, queryUserRole, (currentPageNo - 1) * pageSize, pageSize);
        model.addAttribute("userList", userList);
        for (User user : userList) {
            model.addAttribute("user", user);
        }

        model.addAttribute("totalCount", userTotalCount);
        model.addAttribute("currentPageNo", currentPageNo);
        model.addAttribute("totalPageCount", totalPageCount);
        model.addAttribute("queryUserName", queryname);
        model.addAttribute("queryUserRole", queryUserRole);
        return "userlist";
    }

    @RequestMapping("/userView")
    public String userView(Model model, HttpServletRequest request) {
        String userCode = request.getParameter("userCode");
        String userName = request.getParameter("userName");
        int gender = Integer.parseInt(request.getParameter("gender"));
        String birthday = request.getParameter("birthday");
        String phone = request.getParameter("phone");
        String address = request.getParameter("address");
        String userRoleName = request.getParameter("userRoleName");
        model.addAttribute("userCode", userCode);
        model.addAttribute("userName", userName);
        model.addAttribute("gender", gender);
        model.addAttribute("birthday", birthday);
        model.addAttribute("phone", phone);
        model.addAttribute("address", address);
        model.addAttribute("userRoleName", userRoleName);
        return "userview";
    }

    @RequestMapping("/deluser")
    public void deluser(Model model, HttpServletRequest request, HttpServletResponse response) {
        int userid = Integer.parseInt(request.getParameter("userid"));
        userService.deleteUserById(userid);
        try {
            response.sendRedirect(request.getContextPath() + "/query.do");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
